<template>
  <div class="about">
    <el-button @click="handleAddItem">新建</el-button>
    <!--  -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
    >
      <el-form :model="formData" label-width="80px">
        <!-- <el-form-item label="ID">
          <el-input v-model="formData.id" disabled></el-input>
        </el-form-item> -->
        <el-form-item label="城市">
          <el-input v-model="formData.city"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-input v-model="formData.img"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="formData.date"
            type="date"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!--  -->
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="城市" prop="city"></el-table-column>
      <el-table-column label="时间" prop="date"></el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.img" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="params.size"
      @current-change="currentChange"
    ></el-pagination>
  </div>
</template>
<script>
import axios from 'axios'
import FormDrawer from '@/components/FormDrawer.vue'
import myMxins from '../mixins/index'
export default {
  components: { FormDrawer },
  data() {
    return {
      tableData: [],
      params: {
        page: 1,
        size: 5,
      },
      formData: {
        id: '',
        date: '',
        img: '',
        city: '',
      },
      total: 0,
      drawer: false,
      title: '',
    }
  },

  mixins: [myMxins],
  methods: {
    sleep() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(true)
        }, 2000)
      })
    },

    handleEdit(index, row) {
      this.title = '编辑'
      // this.drawer = true
      // this.formData = JSON.parse(JSON.stringify(row))

      this.$myDrawer({
        formSlots: {
          component: FormDrawer,
          formData: JSON.parse(JSON.stringify(row)),
          listeners: {
            onSuccess: (res) => {
              console.log('我是编辑参数', res)
              this.submitForm()
              // this.
            },
          },
        },
      })
    },
    async handleDelete(index, row) {
      const res = await axios.get('/api/deleteItem', { params: { id: row.id } })
      console.log('res:', res)
      if (res.data.code === 0) {
        this.$message.success('删除成功')
      }
    },
    currentChange(page) {
      this.params.page = page
      this.getData()
    },
    // 调用新建弹出框
    handleAddItem() {
      this.title = '增加数据'
      // this.drawer = true
      // about组件 -> onSuccess js -> new Drawer({}) myDerawer.vue
      // -> components is formDrawer click  ->submit =>click执行onSuccess
      this.$myDrawer({
        formSlots: {
          component: FormDrawer,
          listeners: {
            onSuccess: (res) => {
              console.log('我是接收到的参数', res)
              this.submitForm()
            },
          },
        },
      })
    },
    handleClose() {
      this.drawer = false
    },
    async submitForm() {
      if (this.title === '增加数据') {
        const res = await axios.post('/api/addItem', this.formData)
        if (res.data.code === 0) {
          this.$message.success('新增成功')
        }
      } else {
        const res = await axios.post('/api/editItem', this.formData)
        if (res.data.code === 0) {
          this.$message.success('编辑成功')
        }
      }
      this.getData()
    },
  },
}
</script>
